import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;


public class SportsLeaderBoardHttp implements HttpHandler {
	 
	private SportsLeaderBoardView view;

	public void handle(HttpExchange t) throws IOException {
//		InputStream is = t.getRequestBody();
		System.out.println("HTTP request recieved from " + t.getRemoteAddress());
		String response = view.getView();
		t.sendResponseHeaders(200, response.length());
		OutputStream os = t.getResponseBody();
		os.write(response.getBytes());
		os.close();
     }
	
	public void startServer(int port, SportsLeaderBoardView view) throws IOException {
		this.view = view;
		HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
		server.createContext("/", this);
		server.setExecutor(null); // creates a default executor
		server.start();
		System.out.println("HTTP server started, listening on port " + port);
	}
}
